package com.droidblackbox.history;

import java.io.File;
import java.io.IOException;
import java.util.List;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import com.droidblackbox.R;
import com.droidblackbox.info.BlackBox;
import com.droidblackbox.manager.FileManager;
import com.droidblackbox.manager.SystemManager;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class HistoryMapViewPlayer extends MapActivity {

	private MapView mapView = null;
	private MyHistoryOverlay historyOverlay = null;

	private String fileName;
	private List<Object> list = null;

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_view);

		new AddAsyncTask().execute();

	}

	private static final int DIALOG_PROGRESS = 1;
	private static final int MAX_PROGRESS = 0x70;
	private ProgressDialog progressDialog = null;

	@Override
	public Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_PROGRESS:

			progressDialog = new ProgressDialog(HistoryMapViewPlayer.this);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setTitle("DrodiBlackBox");
			progressDialog.setMessage("Loading..... Please wait..");
			progressDialog.setCancelable(false);
			progressDialog.setMax(MAX_PROGRESS); // Max값 100
			return progressDialog;

		}
		return null;
	}

	class AddAsyncTask extends AsyncTask<Void, Integer, Void> {

		@Override
		protected void onPreExecute() {
			initCompoment();

			showDialog(DIALOG_PROGRESS);

			progressDialog.setProgress(0);
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... unused) {

			runOnUiThread(new Runnable() {

				public void run() {
					while(mapView.getOverlays().isEmpty()) {
						
					}
				}
			});
			publishProgress(MAX_PROGRESS);
			return (null);
		}

		@Override
		protected void onProgressUpdate(Integer... progress) {
			progressDialog.setProgress(progress[0]);
		}

		@Override
		protected void onPostExecute(Void unused) {
			dismissDialog(DIALOG_PROGRESS);
			progressDialog.dismiss();

		}

		protected void onCancelled() {
			super.onCancelled();
		}
	}

	private void initCompoment() {
		Intent intent = getIntent();

		String s = intent.getStringExtra(FileManager.SELECT_FILE);
		fileName = s.replaceAll(FileManager.MEDIA_EXTENSION,
				FileManager.DATA_EXTENSION);

		try {
			File forder = FileManager.getFile(FileManager.FOLDER_BLACK_BOX);
			File f = new File(forder, fileName);
			list = FileManager.readerListObject(f);

			for (Object o : list) {
				System.out.println(getBlackBox(o).toString());
			}

			mapView = (MapView) findViewById(R.id.mapView);
			MapController mapController = mapView.getController();
			mapController.setZoom(13);
			mapView.setSatellite(false);
			mapView.setBuiltInZoomControls(true);

			int lat = getBlackBox(list.get(0)).getLatitude();
			int lon = getBlackBox(list.get(0)).getLongitude();

			GeoPoint geoPoint = new GeoPoint(lat, lon);
			mapController.setCenter(geoPoint);

			historyOverlay = new MyHistoryOverlay(this, list);
			mapView.getOverlays().add(historyOverlay);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ArrayIndexOutOfBoundsException e) {
			String msg = getResources().getString(R.string.mapview_error_text);
			SystemManager.getInstance().showToast(HistoryMapViewPlayer.this,
					msg);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private BlackBox getBlackBox(Object object) throws ClassCastException {
		return (BlackBox) object;
	}

}
